import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [ 
    {
        path: '/',
        name: 'Home',
        // redirect: '/index',
        component: () => import(/* webpackChunkName: "home" */ '../views/Home.vue'),
        children: [
            {
                path: '/',
                name: 'Index',
                component: () => import(/* webpackChunkName: "index" */ '../views/main/Index.vue'),
            },
            {
                path: '/login',
                name: 'Login',
                component: () => import(/* webpackChunkName: "login" */ '../views/Login.vue'),
            },
            {
                path: '/forget_password',
                name: 'ForgetPassword',
                component: () => import(/* webpackChunkName: "forget_password" */ '../views/ForgetPassword.vue'),
            },
            {
                path: '/register',
                name: 'Register',
                component: () => import(/* webpackChunkName: "register" */ '../views/Register.vue'),
            },
            {
                path: '/index',
                name: 'Index',
                component: () => import(/* webpackChunkName: "index" */ '../views/main/Index.vue'),
            },
            {
                path: '/goods',
                name: 'Goods',
                redirect: '/goods/goodsList',
                component: () => import(/* webpackChunkName: "goods" */ '../views/main/goods/Index.vue'),
                children: [
                    {
                        path: '/goods/goodsList',
                        name: 'GoodsIndex',
                        component: () => import(/* webpackChunkName: "goods_index" */ '../views/main/goods/Goods.vue'),
                    },
                    {
                        path: '/goods/goods_purchase',
                        name: 'GoodsPurchase',
                        component: () => import(/* webpackChunkName: "goods_purchase" */ '../views/main/goods/GoodsPurchase.vue'),
                    },
                    {
                        path: '/goods/goods_history',
                        name: 'GoodsHistory',
                        component: () => import(/* webpackChunkName: "goods_history" */ '../views/main/goods/GoodsHistory.vue'),
                    },
                    {
                        path: '/goods/:id',
                        name: 'GoodsDetail',
                        component: () => import(/* webpackChunkName: "goods_detail" */ '../views/main/goods/Detail.vue'),
                    }
                ]
            },
            {
                path: '/member',
                name: 'Member',
                redirect: '/member/index',
                component: () => import(/* webpackChunkName: "member" */ '../views/main/member/Index.vue'),
                children: [
                    {
                        path: '/member/index',
                        name: 'MemberIndex',
                        meta:{requireAuth: true,},
                        component: () => import(/* webpackChunkName: "member_index" */ '../views/main/member/Member.vue'),
                    },
                    {
                        path: '/member/send_order',
                        name: 'SendOrder',
                        meta:{requireAuth: true,},
                        component: () => import(/* webpackChunkName: "send_order" */ '../views/main/member/SendOrder.vue'),
                    },
                    {
                        path: '/member/order',
                        name: 'Order',
                        meta:{requireAuth: true,},
                        component: () => import(/* webpackChunkName: "order" */ '../views/main/member/Order.vue'),
                    },
                    {
                        path: '/member/receipt',
                        name: 'Receipt',
                        meta:{requireAuth: true,},
                        component: () => import(/* webpackChunkName: "receipt" */ '../views/main/member/Receipt.vue'),
                    },
                    {
                        path: '/member/recharge',
                        name: 'Recharge',
                        meta:{requireAuth: true,},
                        component: () => import(/* webpackChunkName: "recharge" */ '../views/main/member/Recharge.vue'),
                    },
                    {
                        path: '/member/money_log',
                        name: 'MoneyLog',
                        meta:{requireAuth: true,},
                        component: () => import(/* webpackChunkName: "money_log" */ '../views/main/member/MoneyLog.vue'),
                    },
                    {
                        path: '/member/check_pieces',
                        name: 'CheckPieces',
                        meta:{requireAuth: true,},
                        component: () => import(/* webpackChunkName: "check_pieces" */ '../views/main/member/CheckPieces.vue'),
                    },
                    {
                        path: '/member/receive_order',
                        name: 'ReceiveOrder',
                        meta:{requireAuth: true,},
                        component: () => import(/* webpackChunkName: "receive_order" */ '../views/main/member/ReceiveOrder.vue'),
                    },
                    {
                        path: '/member/vip',
                        name: 'Vip',
                        meta:{requireAuth: true,},
                        component: () => import(/* webpackChunkName: "vip" */ '../views/main/member/Vip.vue'),
                    },
                    {
                        path: '/member/edit_password',
                        name: 'EditPassword',
                        meta:{requireAuth: true,},
                        component: () => import(/* webpackChunkName: "edit_password" */ '../views/main/member/EditPassword.vue'),
                    },
                    {
                        path: '/member/edit_member',
                        name: 'EditMember',
                        meta:{requireAuth: true,},
                        component: () => import(/* webpackChunkName: "edit_member" */ '../views/main/member/EditMember.vue'),
                    },
                    {
                        path: '/member/inviter',
                        name: 'Inviter',
                        meta:{requireAuth: true,},
                        component: () => import(/* webpackChunkName: "inviter" */ '../views/main/member/Inviter.vue'),
                    },
                    {
                        path: '/member/other_site',
                        name: 'OtherSite',
                        component: () => import(/* webpackChunkName: "other_site" */ '../views/main/member/OtherSite.vue'),
                    },
                    {
                        path: '/member/site_apply',
                        name: 'SiteApply',
                        component: () => import(/* webpackChunkName: "site_apply" */ '../views/main/member/SiteApply.vue'),
                    },
                    {
                        path: '/member/interface',
                        name: 'Interface',
                        component: () => import(/* webpackChunkName: "interface" */ '../views/main/member/Interface.vue'),
                    },
                    {
                        path: '/member/teamwork',
                        name: 'Teamwork',
                        component: () => import(/* webpackChunkName: "teamwork" */ '../views/main/member/Teamwork.vue'),
                    },
                    {
                        path: '/member/about',
                        name: 'About',
                        component: () => import(/* webpackChunkName: "about" */ '../views/main/member/About.vue'),
                    },
                    {
                        path: '/member/guide',
                        name: 'Guide',
                        component: () => import(/* webpackChunkName: "guide" */ '../views/main/member/Guide.vue'),
                    },
                    {
                        path: '/member/check_order',
                        name: 'CheckOrder',
                        component: () => import(/* webpackChunkName: "check_order" */ '../views/main/member/CheckOrder.vue'),
                    },
                    {
                        path: '/member/chose_goods',
                        name: 'ChoseGoods',
                        component: () => import(/* webpackChunkName: "chose_goods" */ '../views/main/member/ChoseGoods.vue'),
                    },
                    {
                        path: '/member/stop_area',
                        name: 'StopArea',
                        component: () => import(/* webpackChunkName: "chose_goods" */ '../views/main/member/StopArea.vue'),
                    },
                    {
                        path: '/member/withdraw',
                        name: 'Withdraw',
                        component: () => import(/* webpackChunkName: "chose_goods" */ '../views/main/member/Withdraw.vue'),
                    },
                    {
                        path: '/member/advice',
                        name: 'Advice',
                        component: () => import(/* webpackChunkName: "chose_goods" */ '../views/main/member/Advice.vue'),
                    },
                    {
                        path: '/member/advice_list',
                        name: 'AdviceList',
                        component: () => import(/* webpackChunkName: "chose_goods" */ '../views/main/member/AdviceList.vue'),
                    },
                ]
            },
            {
                path: '/news',
                name: 'News',
                component: () => import(/* webpackChunkName: "member" */ '../views/main/news/Index.vue'),
                children: [
                    {
                        path: '/news/:id',
                        name: 'Detail',
                        component: () => import(/* webpackChunkName: "detail" */ '../views/main/news/Detail.vue'),
                    },
                ]
            },
            {
                path: '/about',
                name: 'Abouts',
                component: () => import(/* webpackChunkName: "about" */ '../views/About.vue'),
            },
            {
                path: '/ad',
                name: 'Ad',
                component: () => import(/* webpackChunkName: "ad" */ '../views/Ad.vue'),
            }
        ]
    },
    {
        path: '/wxLogin',
        name: 'WxLogin',
        component: () => import(/* webpackChunkName: "register" */ '../views/WxLogin.vue'),
    },
    {
        path: '/m_index',
        name: 'M_index',
        component: () => import(/* webpackChunkName: "m_index" */ '../views/mobile/Index.vue'),
    },
    {
        path: '/m_login',
        name: 'M_login',
        component: () => import(/* webpackChunkName: "m_login" */ '../views/mobile/Login.vue'),
    },
    {
        path: '/m_register',
        name: 'M_register',
        component: () => import(/* webpackChunkName: "m_register" */ '../views/mobile/Register.vue'),
    },
    {
        path: '/m_forgetpassword',
        name: 'M_forgetpassword',
        component: () => import(/* webpackChunkName: "m_forgetpassword" */ '../views/mobile/ForgetPassword.vue'),
    },
    {
        path: '/m_goodsall',
        name: 'M_goodsall',
        component: () => import(/* webpackChunkName: "m_goodsall" */ '../views/mobile/GoodsAll.vue'),
    },
    {
        path: '/m_guide',
        name: 'M_guide',
        component: () => import(/* webpackChunkName: "m_guide" */ '../views/mobile/Guide.vue'),
    },
    {
        path: '/m_othersite',
        name: 'M_othersite',
        component: () => import(/* webpackChunkName: "m_othersite" */ '../views/mobile/OtherSite.vue'),
    },
]

const router = new VueRouter({
    mode: 'hash',
    base: process.env.BASE_URL,
    routes
})

router.beforeEach((to, from, next) => {
    if (to.matched.some(r => r.meta.requireAuth)) {
        var token = localStorage.getItem('dftToken');
        if (token) {
            next();
        }
        else {
            next({
                path: '/login',
                query: {redirect: to.path}
            })
        }
    }
    else {
        next();
    }
})

export default router
